package com.crowntit.trigger.listener;

import com.alibaba.fastjson.JSON;
import com.crowntit.api.dto.NotifyRequestDTO;
import com.crowntit.domain.goods.service.IGoodsService;
import com.crowntit.domain.order.adapter.event.PaySuccessMessageEvent;
import com.crowntit.domain.order.model.entity.GroupBuyNotifyEntity;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class OrderPaySuccessListener {

    @Resource
    private IGoodsService goodsService;

//    @Subscribe
//    public void handleEvent(String paySuccessMessageJson){
//        log.info("收到支付成功消息，可以做接下来的事情，如；发货、充值、开户员、返利 {}", paySuccessMessageJson);
//
//        PaySuccessMessageEvent.PaySuccessMassage paySuccessMessage = JSON.parseObject(paySuccessMessageJson, PaySuccessMessageEvent.PaySuccessMassage.class);
//        try {
//            goodsService.changeOrderDealDone(paySuccessMessage.getOrderId());
//        } catch (Exception e) {
//            throw new RuntimeException("订单完成，修改状态时发生错误", e);
//        }
//    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "${spring.rabbitmq.config.consumer.topic_order_pay_success.queue}"),
                    exchange = @Exchange(value = "${spring.rabbitmq.config.consumer.topic_order_pay_success.exchange}", type = ExchangeTypes.TOPIC),
                    key = "${spring.rabbitmq.config.consumer.topic_order_pay_success.routing_key}"
            ))
    public void listener(String message){
        log.info("接收到MQ消息，topic: team_success，message: {}", message);

        try {
            PaySuccessMessageEvent.PaySuccessMassage paySuccessMessage = JSON.parseObject(message, PaySuccessMessageEvent.PaySuccessMassage.class);
            goodsService.changeOrderDealDone(paySuccessMessage.getOrderId());
        } catch (Exception e) {
            throw new RuntimeException("订单完成，修改状态时发生错误", e);
        }
    }
}
